package com.siashan.unipay4j.ali;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.siashan.unipay4j.core.IPayConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 支付宝支付抽象类
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public abstract class AbstractAliPay {

    /**
     * 客户端
     */
    protected AlipayClient alipayClient;
    /**
     * 支付配置
     */
    protected AliPayConfig payConfig;

    public AbstractAliPay() {
        payConfig = new AliPayConfig();
        payConfig.setAppId(AliConfiguration.getAppId());
        payConfig.setSignType(AliConfiguration.getSignType());
        payConfig.setNotifyUrl(AliConfiguration.getNotifyUrl());
        payConfig.setFormat(AliConfiguration.getFormat());
        payConfig.setCharset(AliConfiguration.getCharset());
        payConfig.setMerchantPrivateKey(AliConfiguration.getMerchantPrivateKey());
        payConfig.setAlipayPublicKey(AliConfiguration.getAlipayPublicKey());
        payConfig.setGatewayUrl(AliConfiguration.getGatewayUrl());
        payConfig.setUseCert(AliConfiguration.getUseCert());
        payConfig.setAlipayCertPath(AliConfiguration.getAlipayCertPath());
        payConfig.setAlipayRootCertPath(AliConfiguration.getAlipayRootCertPath());
        payConfig.setAppCertPath(AliConfiguration.getAppCertPath());
        initClient();
    }

    public AbstractAliPay(IPayConfig payConfig) {
        this.payConfig = (AliPayConfig) payConfig;
        initClient();
    }


    /**
     * 初始化客户端
     */
    protected void initClient() {
        if (this.payConfig.isUseCert()) {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            //gateway:支付宝网关（固定）https://openapi.alipay.com/gateway.do
            certAlipayRequest.setServerUrl(payConfig.getGatewayUrl());
            //APPID 即创建应用后生成,详情见创建应用并获取 APPID
            certAlipayRequest.setAppId(payConfig.getAppId());
            //开发者应用私钥，由开发者自己生成
            certAlipayRequest.setPrivateKey(payConfig.getMerchantPrivateKey());
            //参数返回格式，只支持 json 格式
            certAlipayRequest.setFormat(payConfig.getFormat());
            //请求和签名使用的字符编码格式，支持 GBK和 UTF-8
            certAlipayRequest.setCharset(payConfig.getCharset());
            //商户生成签名字符串所使用的签名算法类型，目前支持 RSA2 和 RSA，推荐商家使用 RSA2。
            certAlipayRequest.setSignType(payConfig.getSignType());
            //应用公钥证书路径（app_cert_path 文件绝对路径）
            certAlipayRequest.setCertPath(payConfig.getAppCertPath());
            //支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径）
            certAlipayRequest.setAlipayPublicCertPath(payConfig.getAlipayCertPath());
            //支付宝CA根证书文件路径（alipay_root_cert_path 文件绝对路径）
            certAlipayRequest.setRootCertPath(payConfig.getAlipayRootCertPath());
            try {
                this.alipayClient = new DefaultAlipayClient(certAlipayRequest);
            } catch (AlipayApiException e) {
                log.warn("初始化支付宝客户端异常",e);
            }
        } else {
            this.alipayClient = new DefaultAlipayClient(payConfig.getGatewayUrl(),
                    payConfig.getAppId(),
                    payConfig.getMerchantPrivateKey(),
                    payConfig.getFormat(),
                    payConfig.getCharset(),
                    payConfig.getAlipayPublicKey(),
                    payConfig.getSignType());
        }

    }

}
